<%= form_with(model: project, local: true) do |form| %>
  <% if project.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(project.errors.count, "error") %> prohibited this project from being saved:</h2>

      <ul>
      <% project.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="form-group">
    <%= form.label :name %>
    <%= form.text_field :name, class: 'form-control' %>
  </div>

  <div class="form-group">
    <%= form.label :description %>
    <%= form.text_field :description, class: 'form-control' %>
  </div>

  <h4>Tasks</h4>
  <div data-controller="nested-form">
    <template data-target="nested-form.template">
      <%= form.fields_for :tasks, Task.new, child_index: 'New_RECORD' do |task| %>
        <%= render 'nest', form: task%>
      <% end %>
    </template>

    <%= form.fields_for :tasks do |task| %>
      <%= render 'nest', form: task%>
    <% end %>

    <div class="mb-3" data-target='nested-form.links'>
      <%= link_to 'Add Task', '#', class: 'btn btn-outline-primary', data: {action: 'click->nested-form#add_association'} %>
    </div>
  </div>


  <div class="form-group">
    <% if project.persisted? %>
      <div class="float-right">
        <%= link_to 'Destroy', project, method: :delete, class: "text-danger", data: { confirm: 'Are you sure?' } %>
      </div>
    <% end %>

    <%= form.submit class: 'btn btn-primary' %>

    <% if project.persisted? %>
      <%= link_to "Cancel", project, class: "btn btn-link" %>
    <% else %>
      <%= link_to "Cancel", projects_path, class: "btn btn-link" %>
    <% end %>
  </div>
<% end %>
